package com.carbon.controller;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.PhoneUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.carbon.common.core.domain.AjaxResult;
import com.carbon.common.core.redis.RedisCache;
import com.carbon.common.core.text.Convert;
import com.carbon.domain.Carrier;
import com.carbon.domain.Storage;
import com.carbon.service.CarrierService;

import javax.annotation.Resource;
import java.io.Serializable;
import java.lang.reflect.Array;
import java.util.*;
import java.util.concurrent.TimeUnit;

import com.carbon.service.DeliveryService;
import com.carbon.vo.AddressVo;
import com.carbon.vo.SearchParams;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 测试 (Test)表控制层
 *
 * @author makejava
 * @since 2023-09-05 11:37:04
 */
@RestController
@Slf4j
@RequestMapping("/wms/carrier")
public class CarrierController {
    /**
     * 服务对象
     */
    @Resource
    private CarrierService carrierService;
    @Resource
    private RedisCache redisCache;

    @GetMapping("/list")
    public AjaxResult list(SearchParams params) {
        Page<Carrier> page = new Page<>(params.getPageNum(), params.getPageSize());
        LambdaUpdateWrapper<Carrier> lqw = new LambdaUpdateWrapper<Carrier>()
                .eq(ObjectUtil.isNotEmpty(params.getCreateStatus()), Carrier::getCreateStatus, params.getCreateStatus())
                .eq(ObjectUtil.isNotEmpty(params.getUpdateStatus()), Carrier::getUpdateStatus, params.getUpdateStatus());
        String flag = params.getFlag();
        if ("1".equalsIgnoreCase(flag)) {
            lqw.eq(Carrier::getCreateStatus, "0");
        }

        String search = params.getSearch();
        if (ObjectUtil.isNotEmpty(search)) {
            lqw.like(Carrier::getOrderId, search).or()
                    .like(Carrier::getCreateBy, search).or()
                    .like(Carrier::getUpdateBy, search).or()
                    .like(Carrier::getCustomerName, search).or()
                    .like(Carrier::getOutboundId, search).or()
                    .like(Carrier::getCarrier, search);
        }
        String dateTime = params.getDateTime();
        if (ObjectUtil.isNotEmpty(dateTime)) {
            lqw.like(Carrier::getCreateTime, dateTime).or()
                    .like(Carrier::getUpdateTime, dateTime);
        }
        page = carrierService.page(page, lqw);
        List<Carrier> records = page.getRecords();
        if (ObjectUtil.isEmpty(records)) {
            return AjaxResult.success();
        }
        return AjaxResult.success()
                .put("rows", records)
                .put("total", page.getTotal());
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public AjaxResult selectOne(@PathVariable Serializable id) {
        Carrier carrier = carrierService.getById(id);
        List<String> split = StrUtil.split(carrier.getAddrCode(),",");
        carrier.setDeliveryAddrList(split);
        return AjaxResult.success(carrier);
    }

    /**
     * 新增数据
     *
     * @param carrier 实体对象
     * @return 新增结果
     */
    @PostMapping
    public AjaxResult insert(@RequestBody Carrier carrier) {
        AjaxResult result = IsLegitiMate(carrier);
        if (result != null) {
            return result;
        }
        final String join = CollUtil.join(carrier.getDeliveryAddrList(), ",");
        carrier.setAddrCode(join);
        carrierService.save(carrier);
        return AjaxResult.success();
    }

    /**
     * 参数校验
     *
     * @param carrier
     * @return
     */
    private static AjaxResult IsLegitiMate(Carrier carrier) {
        if (ObjectUtil.isEmpty(carrier.getCustomerName())) {
            return AjaxResult.error("客户为空");
        }
        if (ObjectUtil.isEmpty(carrier.getDeliveryAddrList())) {
            return AjaxResult.error("收货地址为空");
        }
        if (ObjectUtil.isEmpty(carrier.getContactName())) {
            return AjaxResult.error("联系人为空");
        }
        if (ObjectUtil.isEmpty(carrier.getContactNumber())) {
            return AjaxResult.error("联系电话为空");
        }
        if (!PhoneUtil.isPhone(carrier.getContactNumber())) {
            return AjaxResult.error("手机号格式错误");
        }
        if (ObjectUtil.isEmpty(carrier.getCarrier())) {
            return AjaxResult.error("承运人为空");
        }
        if (ObjectUtil.isEmpty(carrier.getDistance())) {
            return AjaxResult.error("运输距离为空");
        }
        return null;
    }

    /**
     * 修改数据
     *
     * @param carrier 实体对象
     * @return 修改结果
     */
    @PutMapping
    public AjaxResult update(@RequestBody Carrier carrier) {
        carrierService.updateById(carrier);
        return AjaxResult.success();
    }

    /**
     * 修改审核状态
     *
     * @param id
     * @param updateStatus
     * @param comments
     * @return
     */
    @PutMapping("/updateStatus")
    public AjaxResult updateStatus(int id, String updateStatus, String comments) {
        Carrier carrier = new Carrier();
        carrier.setId(id);
        carrier.setUpdateStatus(updateStatus);
        carrier.setComments(comments);
        // 审核通过
        if ("1".equalsIgnoreCase(updateStatus)) {
            carrier.setUpdateStatus("1");
            carrier.setCreateStatus("1");
        } else {
            carrier.setUpdateStatus("0");
            carrier.setCreateStatus("1");
        }
        return carrierService.updateById(carrier) ? AjaxResult.success() : AjaxResult.error();
    }

    /**
     * 删除数据
     *
     * @param
     * @return 删除结果
     */
    @DeleteMapping("{id}")
    public AjaxResult delete(@PathVariable Long id) {
        String s = Convert.toStr(id);
        List<String> split = StrUtil.split(s, ",");
        LambdaUpdateWrapper<Carrier> lqw = new LambdaUpdateWrapper<Carrier>()
                .in(Carrier::getId, split);
        return AjaxResult.success(carrierService.remove(lqw));
    }
}


